import { createApp } from 'vue'
import App from './App.vue'
// import ElementPlus from 'element-plus'
// import 'element-plus/dist/index.css'
import router from './router'

import * as ElementPlusIconVue from '@element-plus/icons'

import './assets/less/index.less'
import store from "./store/index.js";
import './api/mock.js'
import api from './api/api'

const app = createApp(App);

for (const [key,component] of Object.entries(ElementPlusIconVue)) {
    app.component(key,component)
}

app.use(store)
app.use(router)

//store.commit("addMenu",router)

//检查路径，无效路径跳转到home
function checkRouter(path) {
    let hasCheck = router.getRoutes().filter(route => route.path == path).length
    if (hasCheck) {
        return true
    } else {
        return false
    }
}

//如果没有token则跳到登录页
router.beforeEach((to, from, next) => {
    store.commit('getToken')
    const token = store.state.token
    if (!token && to.name !== 'login') {
        next({ name: 'login' })
    } else if (!checkRouter(to.path)) {
        next({ name: 'home' })
    } else {
        next()
    }
})

app.config.globalProperties.$api = api
app.mount('#app')
